const mongoose = require('mongoose');
const { db, rootSrc } = require('../config');
const PATH = require('path');
const FS = require('fs');
const directory = PATH.join(rootSrc, '/schema');
const filesNameArr = FS.readdirSync(directory);
const url = `mongodb://${db.domain || "127.0.0.1"}:${db.port || "27017"}/${db.name}`;
return true;
mongoose.connect(url, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false,
  useCreateIndex: true
});
mongoose.connection.on('error', function (error) {
  console.log('数据库test连接失败：' + error);
});
mongoose.connection.on('open', function () {
  console.log(db.name, ': 数据库test连接成功');
});
let Schema = {};
filesNameArr.forEach(filename => {
  if (filename !== 'index.js') {
    let filePath = PATH.join(directory, filename);
    if (FS.statSync(filePath).isFile()) {
      let modelObject = require(filePath);
      let name = filename.replace(/\..*/g, '');
      let model = new mongoose.Schema(modelObject);
      Schema[name] = mongoose.model(name.charAt(0).toUpperCase() + name.slice(1), model);
    }
  }
})
module.exports = Schema;